//
//  HCSureSuccess.m
//  HappyChildren
//
//  Created by WillyZhao on 2017/5/23.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCSureSuccess.h"

#import "ZXPAutoLayout.h"


@interface HCSureSuccess () {
    UIView *backGroundView;             //背景
    UIImageView *centerView;                 //内容
    UIImageView *logoImageView;
    NSArray *buttons;
    BOOL reppp;
}

@end

@implementation HCSureSuccess

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        //初始化背景视图
        backGroundView = [UIView new];
        backGroundView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
        [self addSubview:backGroundView];
        
        [backGroundView zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) {
            layout.edgeInsets(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
        
        UIImage *image = [UIImage imageNamed:@"预约成功"];
        
        centerView = [[UIImageView alloc] initWithImage:image];
        centerView.userInteractionEnabled = YES;
        [self addSubview:centerView];
        
        [centerView zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) {
            layout.edgeInsets(UIEdgeInsetsMake((YBKAppFrame.size.height- image.size.height)/2.0, (YBKAppFrame.size.width-image.size.width)/2.0, (YBKAppFrame.size.height-image.size.height)/2.0, (YBKAppFrame.size.width-image.size.width)/2.0));
        }];
        
        UIButton *btn = [UIButton new];
        btn.backgroundColor = [UIColor clearColor];
        [btn addTarget:self action:@selector(sureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [centerView addSubview:btn];
        
        
        [btn zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) {
            layout.edgeInsets(UIEdgeInsetsMake(image.size.height - 31, 0, 0, 0));
        }];
        
    }
    return self;
}

-(void)sureBtnAction:(UIButton *)btn {
    [self removeFromSuperview];
//    if (self.delegate && [self.delegate respondsToSelector:@selector(didClicked)]) {
//        [self.delegate didClicked];
//    }
    if (self.block) {
        self.block();
    }
}

-(void)showAnimateSuccess:(BOOL)isSuccess block:(DismissBloc)block{
    self.block = block;
    if (isSuccess) {
        centerView.image = [UIImage imageNamed:@"预约成功"];
    }else{
        centerView.image  = [UIImage imageNamed:@"预约失败"];
    }
    
    UIWindow *win = [[[UIApplication sharedApplication] delegate] window];
    [win addSubview:self];
    
    
    [UIView animateWithDuration:.1f animations:^{
        [self animationWithView:centerView duration:.3f];
    } completion:^(BOOL finished) {
        
    }];
}



- (void)animationWithView:(UIView *)view duration:(CFTimeInterval)duration{
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = duration;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    
    animation.values = values;
    animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
    
    [view.layer addAnimation:animation forKey:nil];
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
